package com.itheima.edu.info.manger.dao;
import com.itheima.edu.info.manger.domain.Student;
public class StudentDao implements BaseStudentDao{
    // 创建学生对象数组
    private static   Student[] students=new Student[5];
    static {
        Student stu1=new Student("heima001","张三","23","1999-11-11");
        Student stu2=new Student("heima002","李四","24","1998-11-11");
        students[0]=stu1;
        students[2]=stu2;
    }
    // 查看学生方法
    public  Student[] findAllStudent() {
        return students;
    }
//删除学生
    public  void deleteStudentById(String did) {
        int index=getIndex(did);
        students[index]=null;
    }
//修改学生
    public  void updateStudentById(String uid,Student newStu) {
        int index = getIndex(uid);
        students[index]=newStu;

    }
    // 添加学生方法
    public  boolean addStudent(Student stu) {
        //2.1 定义变量index为-1，假设数组已经全部存满，没有null的元素
        int index=-1;
        //2.2 遍历数组取出每一个元素，判断是否是null
        //2.3 如果为null，让index变量记录当前索引位置，并使用break结束循环遍历
        for (int i = 0; i < students.length; i++) {
            Student student = students[i];
            if (student==null){
                index=i;
                break;
            }
        }
        // 3. 返回是否添加成功的boolean类型状态
        if (index==-1){
            return false;
        }else{
            students[index]=stu;
            return true;
        }
    }
    //获取索引
    public  int getIndex(String id){
        int index=-1;
        for (int i = 0; i < students.length; i++) {
            Student stu= students[i];
            if (stu!=null&&stu.getId().equals(id)){
                index=i;
                break;
            }
        }
        return index;
    }
}
